{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "8bf64a76",
   "metadata": {},
   "source": [
    "----------------------------  \n",
    "* 时间：2022/10/24 week10  \n",
    "* 记录人：江柯廷  \n",
    "----------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9fd8f588",
   "metadata": {},
   "source": [
    "# 本周学习目标  \n",
    "> 1. while循环  \n",
    "> 2. 购物车循环"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7850c3d8",
   "metadata": {},
   "source": [
    "## 1. while循环  \n",
    "* 回顾： for循环： for + range() 可以指定循环的次数——>关于网页URL队列的抓取实践  \n",
    "* while循环： 当某种条件成立时，停止循环  \n",
    "* while Ture: 无限循环/死循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "839448ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你是风儿我是沙，缠缠绵绵到天涯... 0\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 1\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 2\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 3\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 4\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 5\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 6\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 7\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 8\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 9\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 10\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 11\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 12\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 13\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 14\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 15\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 16\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 17\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 18\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 19\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 20\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 21\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 22\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 23\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 24\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 25\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 26\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 27\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 28\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 29\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 30\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 31\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 32\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 33\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 34\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 35\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 36\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 37\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 38\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 39\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 40\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 41\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 42\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 43\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 44\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 45\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 46\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 47\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 48\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 49\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 50\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 51\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 52\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 53\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 54\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 55\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 56\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 57\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 58\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 59\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 60\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 61\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 62\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 63\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 64\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 65\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 66\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 67\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 68\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 69\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 70\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 71\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 72\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 73\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 74\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 75\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 76\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 77\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 78\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 79\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 80\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 81\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 82\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 83\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 84\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 85\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 86\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 87\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 88\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 89\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 90\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 91\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 92\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 93\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 94\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 95\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 96\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 97\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 98\n",
      "你是风儿我是沙，缠缠绵绵到天涯... 99\n"
     ]
    }
   ],
   "source": [
    "# 1.指定次数\n",
    "count = 0\n",
    "while True:  \n",
    "    print(\"你是风儿我是沙，缠缠绵绵到天涯...\",count)\n",
    "    count = count+1\n",
    "    if count == 100:\n",
    "        break # break 终止循环 \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5fac98d1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "账号:1215\n",
      "密码:13545\n",
      "账号不存在或密码不正确!请重新输入！您还有2次尝试的机会\n",
      "账号:1684\n",
      "密码:51\n",
      "账号不存在或密码不正确!请重新输入！您还有1次尝试的机会\n",
      "账号:124\n",
      "密码:12\n",
      "账号不存在或密码不正确!请一小时后再来尝试\n"
     ]
    }
   ],
   "source": [
    "# 2. while True循环，可以让用户重复输入\n",
    "user01 = \"admin\"\n",
    "password01 = \"123456\"\n",
    "count = 0 #计数器\n",
    "\n",
    "while True:\n",
    "    username = input(\"账号:\")\n",
    "    password = input(\"密码:\")\n",
    "    if user01 == username and password01 == password:\n",
    "        print(\"登陆成功~欢迎+user01\")\n",
    "        break #终止循环\n",
    "    # 情景二：账号不存在或密码不正确，用户可以输入三次，三次之后一小时之后再来尝试\n",
    "    else:\n",
    "        if count<2:\n",
    "              print(\"账号不存在或密码不正确!请重新输入！您还有\"+str(2-count)+\"次尝试的机会\")\n",
    "        else:\n",
    "            print(\"账号不存在或密码不正确!请一小时后再来尝试\")\n",
    "            break\n",
    "        count = count+1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cb3250cb",
   "metadata": {},
   "source": [
    "## 2.购物车项目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "9d83a735",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "for i in range(4):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "f531bdb5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入余额：30\n",
      "-------------商品列表-----------\n",
      "商品序号 (0): 蜜雪 6元\n",
      "商品序号 (1): 蜜桃 10元\n",
      "商品序号 (2): 芒果 12元\n",
      "商品序号 (3): 葡萄 8元\n",
      "请输入商品编号：3\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for -: 'str' and 'int'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "Input \u001b[1;32mIn [56]\u001b[0m, in \u001b[0;36m<cell line: 13>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     19\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m user_choice\u001b[38;5;241m.\u001b[39misdigit():\n\u001b[0;32m     20\u001b[0m     \u001b[38;5;66;03m# 情景一：余额大于商品价格\u001b[39;00m\n\u001b[0;32m     21\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m Ailpay \u001b[38;5;241m>\u001b[39m\u001b[38;5;241m=\u001b[39m product_list[\u001b[38;5;28mint\u001b[39m(user_choice)][\u001b[38;5;241m1\u001b[39m]:\n\u001b[1;32m---> 22\u001b[0m         Alipay \u001b[38;5;241m=\u001b[39m \u001b[43mAlipay\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[43m  \u001b[49m\u001b[43mproduct_list\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;28;43mint\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43muser_choice\u001b[49m\u001b[43m)\u001b[49m\u001b[43m]\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\n\u001b[0;32m     23\u001b[0m         shopping_car\u001b[38;5;241m.\u001b[39mappend(product_list[\u001b[38;5;28mint\u001b[39m(user_choice)])\n\u001b[0;32m     24\u001b[0m         \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m商品购买成功，正在制作中...您的余额还有\u001b[39m\u001b[38;5;132;01m%s\u001b[39;00m\u001b[38;5;124m元\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;241m%\u001b[39m(Alipay))\n",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for -: 'str' and 'int'"
     ]
    }
   ],
   "source": [
    "# 1.运行程序惠普，让用户输入支付宝余额✔， 然后打印我们的商品列表给用户.\n",
    "product_list = [\n",
    "    [\"蜜雪\",6],\n",
    "    [\"蜜桃\",10],\n",
    "    [\"芒果\",12],\n",
    "    [\"葡萄\",8]\n",
    "]\n",
    "shopping_car = []\n",
    "\n",
    "Ailpay = int(input(\"请输入余额：\"))\n",
    "\n",
    "while True:\n",
    "    print(\"-------------商品列表-----------\")\n",
    "    for i in range(4):\n",
    "        print(\"商品序号\",\"(\"+str(i)+\"):\",product_list[i][0],str(product_list[i][1])+\"元\") # 格式化输出\n",
    "    # 2.让用户输入商品编号进行商品的购买：直接用商品列表的index作为商品编号\n",
    "    user_choice = input(\"请输入商品编号：\")\n",
    "    # 情景1：用户正在购买，输入商品编号\n",
    "    if user_choice.isdigit():\n",
    "        # 情景一：余额大于商品价格\n",
    "        if Ailpay >= product_list[int(user_choice)][1]:\n",
    "            Alipay = Alipay - product_list[int(user_choice)][1]\n",
    "            shopping_car.append(product_list[int(user_choice)])\n",
    "            print(\"商品购买成功，正在制作中...您的余额还有%s元\"%(Alipay))\n",
    "        # 情景二：余额小于商品价格\n",
    "        else:\n",
    "            print(\"这点钱都没有还想喝蜜雪冰城\")\n",
    "    #情景2：用户输入退出，结束购买\n",
    "    elif user_choice == \"q\":\n",
    "        print(shopping_car)\n",
    "        print(\"您的余额还有%s，欢迎下次光临~~\"%(Alipay))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60f4e00a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
